﻿package daphne.noterun.utilities 
{
	import br.com.stimuli.loading.loadingtypes.LoadingItem;
	import daphne.noterun.events.MidiLoadEvent;
	import daphne.noterun.events.MyEventDispatcher;
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	import com.maccherone.urlmultiloader.URLMultiLoader;
	import org.si.sion.midi.SMFData;
	import flash.net.URLLoaderDataFormat;
	import flash.events.IOErrorEvent;
	import br.com.stimuli.loading.BulkLoader;
	import br.com.stimuli.loading.BulkProgressEvent;
	import br.com.stimuli.loading.loadingtypes.URLItem;
	import br.com.stimuli.loading.loadingtypes.BinaryItem;
	
	/**
	 * ...
	 * @author Daphne
	 */
	public class MidiBin 
	{
		private static const BASE:String = "midi/";
		public static const LEVEL_1 = BASE + "test.mid";
		public static const LEVEL_2 = BASE + "figleaf.mid";
		public static const LEVEL_3 = BASE + "bach_846.mid";
		
		private static const PREDEFINED_LEVELS:Array = [ LEVEL_1, LEVEL_2, LEVEL_3 ];
		
		//maps pathes to SMFData
		var midiDataDict:Dictionary;
		
		var loader:BulkLoader;
		
		public function MidiBin()
		{
			BulkLoader.registerNewType(".mid", "Midi", BinaryItem);
			
			this.midiDataDict = new Dictionary();
			
			//var urlMultiLoader:URLMultiLoader = new URLMultiLoader();
			loader = new BulkLoader("main");
			
			var fileDir:String;
			for each (fileDir in PREDEFINED_LEVELS)
			{
				loader.add(fileDir, {id:fileDir});
				//urlMultiLoader.addURLRequest(fileDir, new URLRequest(fileDir) , null, URLLoaderDataFormat.BINARY);
			}
			
			trace(" LOADING MIDI FILES ");
			//urlMultiLoader.addEventListener(Event.COMPLETE, allFilesLoaded);
			//urlMultiLoader.addEventListener(IOErrorEvent.IO_ERROR, onError)
			//urlMultiLoader.load();
			
			loader.addEventListener(BulkLoader.PROGRESS, onAllItemsProgress);
            loader.addEventListener(BulkLoader.COMPLETE, allFilesLoaded);
			loader.start();
		}
		
		private function allFilesLoaded(event:Event):void
		{
			trace(" LOADING FINISHED ");
			//var data:Object = (event.target as URLMultiLoader).data;
			//var fileDir:String;
			//for each (fileDir in PREDEFINED_LEVELS)
			//{
				//var smfData:SMFData = new SMFData();
				//smfData.loadBytes(data[fileDir]);
				//midiDataDict[fileDir] = smfData;
			//}
			
			
			var fileDir:String;
			for each (fileDir in PREDEFINED_LEVELS)
			{		
				var byteArray:ByteArray = loader.getBinary(fileDir);
				
				var smfData:SMFData = new SMFData();
				smfData.loadBytes(byteArray);
				midiDataDict[fileDir] = smfData;
			}
			
			
			MyEventDispatcher.getInstance().dispatchEvent(new MidiLoadEvent(true));
		}

		public function onAllItemsProgress(evt : BulkProgressEvent) : void
		{
            trace(evt.loadingStatus());
        }
		
		private function onError(event:Event)
		{
			trace(" FAILURE ");
			trace(event.toString());
			//MyEventDispatcher.getInstance().dispatchEvent(new MidiLoadEvent(false));
		}
		
		public function addAMidiFile(url:String)
		{
			var urlLoader:URLLoader = new URLLoader();
			urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
			urlLoader.load(new URLRequest(url));
			urlLoader.addEventListener(Event.COMPLETE, fileLoaded);
		}
		
		private function fileLoaded(event:Event)
		{
			var urlLoader:URLLoader =  event.target as URLLoader;
			//urlLoa
		}
		
		public function getDataFor(name:String):SMFData
		{
			return this.midiDataDict[name];
		}
	}

}